

#ifndef   __MODEM_SERVICE
#define   __MODEM_SERVICE

#define   MODEM_PORT									1
#define   UPDATE_RSSI_PERIOD						1000	//*10ms
#define   SMS_FOR_USER_MAX							16
#define   SMS_FOR_EXT_USER_MAX					32           

#define   SMS_HEADER_PHONE_NUMBER_OFFSET		19		        

/* Sender Staus */
#define   SUCCESS		1
#define   FAIL			0xFE

#define   SMS_BUFFER_SIZE		164

enum
{
	INCOMING_CALL_EVENT__PHONE_FOUND = 10,
	INCOMING_CALL_EVENT__PHONE_NOT_FOUND,	
};

extern unsigned char IncomingCall_Event;

// +CMGR: "REC READ","+94786075911",,"06/09/16,13:14:23+00"

/* +CMGR: "REC READ","+972523974781",,"06/05/22,16:59:01+12" */
typedef union
{ 
		unsigned char Data[44];
		struct 
		{
				//unsigned char CMGR[19]; //+CMGR: "REC READ","
				unsigned char PhoneNumber[20]; //+972523974963
				//unsigned char Separator[3];	//",,
				unsigned char DateTime[24];//"06/05/16,15:10:11+12"
		}Field;
				
}SMS_HEADER;


typedef struct
{
		//unsigned char IDX;		  /* Integer type, call identification number */
		//unsigned char DIR;		  /* 0 - Mobile originated call(MO), 1 - Mobile termonated call(MT) */
		unsigned char CallState;
		unsigned char Mode; 		  		/* Fax, Voice, Data */
		unsigned char PhoneNumber[17];
		unsigned char NumberType; 		/* 129 - Local, 145 - International number with access character + */
		//unsigned char Alpha[8];		  		/* Text representation of the phone book entry. */
				
}LIST_CURRENT_CALLS;

//+CMGR: "REC READ","+972523974781",,"06/05/14,15:15:49+12"

/* typedef struct  */
/* { */
/* 		unsigned char Phone[12]; */
/* 		unsigned char TimeDate[20]; */

/* }SMS; */
	

//LIST_CURRENT_CALLS ListCurrentCalls;

#define   READY 		0
#define   NOT_BUSY	0

/* Registration function */
enum
{
	/* Registration States */
	CHECK_MODEM = 70,
	CHECK_MODEM_TYPE,
	CHECK_NMS_PHONE_NUMBER_PREV,
	CHECK_NMS_PHONE_NUMBER,
	CHECK_SIM_NUMBER,
	SIM_WAS_CHANGED,				  /* 75 */
	NETWORK_REGISTRATION,
	WAIT_FOR_ANSWER,
	REGISTRATION_FAIL,
	END_OF_REGISTRATION,
	REGISTRATION_RESET,			  /* 80 */
	SET_LOCATION_PARAMETERS,
	SET_TIME,
	SET_TIME_STEP_1,
	SET_TIME_STEP_2,
	SET_TIME_STEP_3,
	SET_TIME_STEP_4,
	SET_REAL_CLOCK_IN_MODEM,
	GET_MY_PHONE_NUMBER,
	GET_MY_PHONE_NUMBER_STEP_1,
	GET_MY_PHONE_NUMBER_STEP_2,  /* 90 */
	GET_MY_PHONE_NUMBER_STEP_3,
	GET_MY_PHONE_NUMBER_STEP_4,
	GET_MY_PHONE_NUMBER_STEP_5,
	GET_MY_PHONE_NUMBER_STEP_6,
	SET_EXTENDED,
	OTHER_PARAMETERS,
	END_OF_SET,

	/* Responces */
	MODEM_OK,
	MODEM_NOT_ANSWER,
	SIM_NUMBER_OK,					  /* 100 */
	NETWORK_REGISTRATION_OK,
	PARAMETERS_OK,
	SET_TIME_OK,
	CHECK_PHONE_NUMBER,
	SET_TIME_FAIL,
	CHECK_NMS_PHONE_NUMBER_FAIL,
	CHECK_NMS_PHONE_NUMBER_OK,
	GET_MY_PHONE_NUMBER_OK,
	REGISTRATION_OK,
	NO_CARRIER,						  /* 110 */
	WRONG_NUMBER,
	RSSI_UPDATED,
	/* Modem State */
	BUSY,
	/* Modem Type */
	MOTOROLA,
	TELIT,
	/* Modem Owners */
	BUSY_LCC_MONITOR,
	BUSY_SMS_PARSING,
	BUSY_RT_CLOCK,
	BUSY_RSSI_MANAGER,
	BUSY_DIALER,					  /* 120 */
	BUSY_DIALER_,					  /* 121 */
	BUSY_DTMF,
	BUSY_VOICE_MAIL,
	BUSY_SMS_READ,
	BUSY_SMS_MONITOR,
	BUSY_COMMAND_TRANSLATOR,
	BUSY_COMMAND_PROCESSOR,
	BUSY_SMS_MENU,
	BUSY_SHOW_SMS,
	BUSY_SMS_COMMAND_DELETE,	  /* 130 */
	BUSY_COMMAND_HANDLER,		  /* ?? */
	BUSY_SET_TIME,
	BUSY_SMS_CONTROL_REC_READ,
	BUSY_GET_MY_PHONE_NUMBER,
	BUSY_DATA_MODE_SERVICE,
	BUSY_START_CDR,
	BUSY_START_CDR_,
	BUSY_SMS_GET_LIST,			  /* 138 */
	BUSY_SEND_STATUS,
	BUSY_SIM_WATCH,
	BUSY_SEND_IMSI,
	BUSY_CPIN_PASSWORD,
	BUSY_TELIT_CLCC,
	BUSY_TELIT_CLCC_2,
	BUSY_SMS_SENDER,
	BUSY_NETWORK_REGISTRATION,
	BUSY_INCOMING_CALL_MONITOR,
	BUSY_TE_SIM_OPERATION,
	BUSY_APP_PRINT_SMS,
	/* SMS MessageEditor owners*/
	BUSY_SMS_SERVICE,
	BUSY_MESSAGE_EDITOR,
	BUSY_NEIGHBOR_CELL_MONITOR,
	/* Message Type */
	SMS_MESSAGE,
	SMS_REPLAY,
	CDR_SERVICE_SMS_REPLAY,
	NO_MESSAGES,
	SIM_IS_NOT_VALID,
	ERROR_SMS_NUMBER,
	CLCC_DIALER,						  /* for TELIT only */
	CLCC_INCOM,						  /* for TELIT only */
	
	
};

/* Cell Network Monitoring */
#define   CELL_NETWORK__REGISTRATION_OK	0
#define   CELL_NETWORK__NO_REGISTRATION	1
#define   CELL_NETWORK__MODEM_ERROR			2

extern unsigned char CellNetwork_Event, CellNetwork_Status;

void CellNetwork_Reg_Monitor ( void );

/* LIST CURRENT CALLS 		// see function: "void CallStatusMonitor( void )" */		 
		  										 
  /* Call State */					 
#define 	 ACTIVE			'0'									    
#define 	 HELD				'1'																	   
#define 	 DIALING			'2'					   
#define 	 ALERTING		'3'					   
#define 	 INCOMING		'4'					   
#define 	 WAITING			'5'					   
#define 	 RELEASED		'6'					   
  /* Mode */							   
#define 	 CALL_VOICE		'0'					   
#define 	 CALL_DATA		'1'					   
#define 	 CALL_FAX		'2'			
		   
#define 	 NUMBER_LOCAL	129
#define   NUMBER_INTER	145				    
	  											 
/* END of LIST CURRENT CALLS */

#define   SMS_END_OF_NEW	0xFF

//#define   BUSY			0xFF

extern LIST_CURRENT_CALLS ListCurrentCalls; /* AT+CLCC=1 */
extern unsigned char RSSI;		  /* in % */
extern unsigned char DateAndTime[30];
extern unsigned char near ModemStatus;
extern unsigned char ModemOwner;
//extern bit NEW_SMS_MESSAGE;
extern unsigned char NEW_USER_SMS;
extern near bit LCC_UPDATED;
extern near bit SMS_FULL;
extern near bit DATA_MODE;

extern near bit User_Message;
extern near bit No_Delete_SMS_For_User;

extern unsigned char SMS_User_New_Messages[SMS_FOR_USER_MAX];
extern unsigned char pSMS_User_New_Messages;

extern unsigned char SMS_Delete[SMS_FOR_USER_MAX*2];
extern unsigned char pSMS_Delete;

//extern unsigned char bank1 SMS_NotAccepted[6];
extern unsigned char pSMS_NotAccepted;

extern unsigned char SMS_Monitor_Cycle; /* in sec */

extern unsigned char SMS_Monitor_Status;
//extern unsigned char bank1 SMS_StoreAmount;

extern unsigned char SMS_Buffer[SMS_BUFFER_SIZE];

extern unsigned char SMS_User_Free_Entry;
extern unsigned char SMS_User_New_Message_Count;
extern unsigned char SMS_User_Ext_New_Message_Count;

extern unsigned char RegistrationStep;

extern unsigned char SMS_User_Messages_List[SMS_FOR_EXT_USER_MAX];

#ifdef	MODEM_TYPE__TELIT
extern unsigned char Start_CLCC;
#endif


//extern unsigned int bank1 Message_ID;	  /* use for Set Date and Time */

extern SMS_HEADER SMS_Header;

/* return TRUE if RSSI was updated */
unsigned char Get_RSSI ( unsigned char Period );
unsigned char Registration ( void );
//unsigned char Dialer( unsigned char* PhoneNum );
unsigned char Dialer( void );
unsigned char* DateFormat( unsigned char* Str );
unsigned char Modem_GetRealTimeClock( unsigned char* DateTime, unsigned char Period  );
unsigned char RequestToModem ( const char* Request, const char* ExpectedResponse );
unsigned char SendToModem ( const char* Request, const char* ExpectedResponse, unsigned char Signature );
void CallStatusMonitor( void );
unsigned char Get_VoiceMail_PhoneNumber( unsigned char* VM_PhoneNumber );
void SMS_Monitor( void );
unsigned char SMS_Read( unsigned char* Buffer, unsigned char MessageNumber, unsigned char Signature );
void SMS_Add_Message_For_User ( void );

unsigned char SMS_Get( void );
void SMS_UserService ( void );

/* Return how many items in the list and fill SMS_List with numbers of received SMS messages*/
unsigned char SMS_Get_List( const unsigned char* Read_UnRead, unsigned char* SMS_List  );
unsigned char SMS_Read_Messages ( const unsigned char* Read_UnRead, unsigned char* SMS_List );

void IncomingCallMonitor( void );

//void SIM_Watch( void );
#endif

